package com.icesoft.xsnow.common.core.constant.enums;

import com.icesoft.xsnow.common.core.api.bo.ErrorCode;
import com.icesoft.xsnow.common.core.exception.UnknownEnumException;

/**
 * @program: xsnow
 * @description:
 * @author: xuefeng.gao
 * @create: 2019-05-20 09:44
 **/
public interface IErrorCode {

    /**
     * 转换为ErrorCode(自定义返回消息)
     *
     * @param msg
     * @return
     */
    default ErrorCode convert(String msg) {
        return ErrorCode.builder().httpCode(httpCode()).show(show()).error(name()).msg(msg).build();
    }

    /**
     * 转换为ErrorCode
     *
     * @return
     */
    default ErrorCode convert() {
        return ErrorCode.builder().httpCode(httpCode()).show(show()).error(name()).msg(msg()).build();
    }

    static ErrorCodeEnum getErrorCode(String errorCode) {
        ErrorCodeEnum[] enums = ErrorCodeEnum.values();
        for (ErrorCodeEnum errorCodeEnum : enums) {
            if (errorCodeEnum.name().equalsIgnoreCase(errorCode)) {
                return errorCodeEnum;
            }
        }
        throw new UnknownEnumException("Error: Unknown errorCode, or do not support changing errorCode!\n");
    }

    public int httpCode();

    public String msg();

    public boolean show();

    public String name();

}
